package project.ccso.top.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: AiBinXiao
 * @Description: 日期工具类
 * @Date: 2017/9/14 15:07
 */

public class DateUtil {

	/**
	 * 获取当前日期精确到毫秒的str
	 * @return
     */
	public static String getNowTimeStampStr(){
		return new SimpleDateFormat("yyyyMMddHHmmssSSS") .format(new Date() );
	}

	/**
	 * 获取当前指定格式的日期
	 * @param pattern
	 * @return
	 */
	public static String getCureDay(String pattern) {
		if (pattern==null || "".equals(pattern)) {
			pattern="yyyyMMdd";
		}
		DateFormat format1 = new SimpleDateFormat(pattern);
		String s = format1.format(new Date());
		return s;
	}

	/**
	 * 获取当前时间的毫秒值
	 * @return
	 */
	public static String getCurrentTimeMillisStr() {
		Calendar cal = Calendar.getInstance();
		return cal.getTimeInMillis() + "";
	}

	/**
	 * 将Date日期转换为 yyyy-MM-dd的String日期
	 * @param date
	 * @return
	 */
	public static String dateToString(Date date) {
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		String s = format1.format(date);
		return s;
	}

	/**
	 * 将String日期转换为 yyyy-MM-dd的Date日期
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDate(String dateStr) {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = formatDate.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将Date日期转换为 yyyy-MM-dd HH:mm:ss 的String日期
	 * @param date
	 * @return
	 */
	public static String dateToString2(Date date) {
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = format1.format(date);
		return s;
	}

	/**
	 * 将String日期转换为 yyyy-MM-dd HH:mm:ss 的Date日期
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDate2(String dateStr) {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = formatDate.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将Date日期转换为 yyyy年MM月dd日 的String日期
	 * @param date
	 * @return
	 */
	public static String dateToString3(Date date) {
		DateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日");
		String s = format1.format(date);
		return s;
	}

	/**
	 * 将String日期转换为 yyyy年MM月dd日 的Date日期
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDate3(String dateStr) {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy年MM月dd日");
		Date date = null;
		try {
			date = formatDate.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将Date日期转换为 HH:mm 的String日期
	 * @param date
	 * @return
	 */
	public static String dateToString4(Date date) {
		DateFormat format1 = new SimpleDateFormat("HH:mm");
		String s = format1.format(date);
		return s;
	}

	/**
	 * 将String日期转换为 HH:mm 的Date日期
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDate4(String dateStr) {
		SimpleDateFormat formatDate = new SimpleDateFormat("HH:mm");
		Date date = null;
		try {
			date = formatDate.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将Date日期转换为 yyyy-MM-dd HH:mm 的String日期
	 * @param date
	 * @return
	 */
	public static String dateToString5(Date date) {
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String s = format1.format(date);
		return s;
	}

	/**
	 * 将String日期转换为 yyyy-MM-dd HH:mm 的Date日期
	 * @param dateStr
	 * @return
	 */
	public static Date stringToDate5(String dateStr) {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = null;
		try {
			date = formatDate.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获取某一天日期
	 * 
	 * @param day
	 * @return
	 */
	public static String getLastDate(Integer day) {
		String date = "";
		if (day != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, day);
			Date d = cal.getTime();
			date = sdf.format(d.getTime());
		}
		return date;
	}

	/**
	 * 获取某一周的第一天
	 * 
	 * @param weekFlag: 0为当前周，-1为上周，1为下周，依次类推
	 * @return
	 */
	public static String getWeekFirst(Integer weekFlag) {
		String monday = "";
		if (weekFlag != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, weekFlag * 7);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			monday = sdf.format(cal.getTime());
		}
		return monday;
	}

	/**
	 * 获取某一周的最后一天
	 * 
	 * @param weekFlag: 0为当前周，-1为上周，1为下周，依次类推
	 * @return
	 */
	public static String getWeekEnd(Integer weekFlag) {
		String sunday = "";
		if (weekFlag != null) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, (weekFlag + 1) * 7);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			sunday = sdf.format(cal.getTime());
		}
		return sunday;
	}

	/**
	 * 获取上个月第一天日期
	 * @return
	 */
	public static String getPreviousMonthFirst() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, -1);// 减一个月，变为下月的1号
		// lastDate.add(Calendar.DATE,-1);//减去一天，变为当月最后一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	/**
	 * 获取上个月最后一天日期
	 * @return
	 */
	public static String getPreviousMonthEnd() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, -1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	/**
	 * 获取指定日期Long型的毫秒数Str
	 * @param strDate
	 * @return
	 */
	public static String getDateLongStr(String strDate) {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = formatDate.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime() + "";
	}

	/**
	 * 获取指定日期Long型的毫秒数
	 * @param strDate
	 * @return
	 */
	public static Long getDateLong(String strDate) {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = null;
		try {
			date = formatDate.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime();
	}

	/**
	 * 获取指定日期Long型的毫秒数
	 * @param strDate
	 * @return
	 */
	public static Long getDateLong2(String strDate) {
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = formatDate.parse(strDate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date.getTime();
	}
	
	/**
	 * 日期yyyy-mm-dd
	 * to yyyy-mm-dd hh:mi:ss
	 * @param strDate
	 * @return
	 */
	public static String addDateStart(String strDate){
		return strDate + " 00:00:00";
	}

	
	/**
	 * 日期yyyy-MM-dd
	 * to yyyy-MM-dd hh:mm:ss
	 * @param strDate
	 * @return
	 */
	public static String addDateEnd(String strDate){
		return strDate + " 23:59:59";
	}

	/**
	 * 获取指定日期为星期几
	 * @param date
	 * @return
	 */
	public static Integer getWeek(Date date){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (week_index < 0) {
			week_index = 0;
		}
		return week_index;
	}

	/**
	 * 根据星期索引获取星期几字符串
	 * @param weekIndex
	 * @return
	 */
	public static String getWeekStr(Integer weekIndex){
		String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
		return weeks[weekIndex];
	}

	/**
	 * 获得指定日期的前一天
	 * @param Date
	 * @return
	 */
	public static String getSpecifiedDayBefore(String Date) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(Date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day - 1);
		String dayAfter = sdf.format(c.getTime());
		return dayAfter;
	}

	/**
	 * 获得指定日期的后一天
	 * @param Date
	 * @return
	 */
	public static String getSpecifiedDayAfter(String Date) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(Date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day+1);
		String dayAfter = sdf.format(c.getTime());
		return dayAfter;
	}

	/*public static void main(String[] args) {
		*//*String e=getCureDay("yyyy-MM-dd HH:mm:ss");
		System.out.println(e);
		long s=Calendar.getInstance().getTimeInMillis();
		String d=DateUtil.getLastDate(-7);
		System.out.println(d);
		
		d=DateUtil.getLastDate(-1);
		System.out.println(d);*//*
	}*/
}
